﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class controls_EmbeddedYoutubePlayer : System.Web.UI.UserControl
{
    protected string FormattedYouTubeLink;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (_autoInit)
            initializeEmbeddedYoutubePlayer();
    }
    #region methods

    /// <summary>
    /// YOU HAVE TO SET YoutubeVideoId FIRST!
    /// </summary>
    public void initializeEmbeddedYoutubePlayer()
    {
        StringBuilder YoutubeParams;
        if (_youtubeVideoId != string.Empty)
        {
            YoutubeParams = new StringBuilder();
            YoutubeParams.Append("http://www.youtube.com/v/" + _youtubeVideoId.ToString());
            if (_rel)
                YoutubeParams.Append("&rel=1");
            if (_autoplay)
                YoutubeParams.Append("&autoplay=1");
            if (_loop)
                YoutubeParams.Append("&loop=1");
            if (_enablejsapi)
                YoutubeParams.Append("&enablejsapi=1");
            if (_playerapiid != string.Empty)
                YoutubeParams.Append("&playerapiid=" + _playerapiid);
            if (_disablekb)
                YoutubeParams.Append("&disablekb=1");
            if (_egm)
                YoutubeParams.Append("&egm=1");
            if (_fs)
            {
                FullScreenParameter.Text = allowFullScreenParam;
                YoutubeParams.Append(allowFullScreenEmbedProperty);
            }
            if (!_showsearch)
                YoutubeParams.Append("&showsearch=0");
            if (!_showinfo)
                YoutubeParams.Append("&showinfo=0");
            if (_iv_load_policy != 1)
                YoutubeParams.Append("&iv_load_policy=3");
            if (_cc_load_policy != 0)
                YoutubeParams.Append("&cc_load_policy=1");
            FormattedYouTubeLink = YoutubeParams.ToString();
        }
    }

    #endregion

    #region properties

    private bool _autoInit = false;
    /// <summary>
    /// Set to true if setting player values in the tag.  This allows you 
    /// to skip calling initializeEmbeddedYoutubePlayer() in the code behind.
    /// </summary>
    public bool AutoInit
    {
        get { return _autoInit; }
        set { _autoInit = (bool)value; }
    }

    private bool _rel = true;
    /// <summary>
    /// Sets whether the player should load related videos once playback of 
    /// the initial video starts. Related videos are displayed in the "genie menu" 
    /// when the menu button is pressed. The player search functionality will be 
    /// disabled if rel is set to 0.
    /// </summary>
    public bool rel
    {
        get { return _rel; }
        set { _rel = (bool)value; }
    }
    private bool _autoplay = false;
    /// <summary>
    /// Sets whether or not the initial video will autoplay 
    /// when the player loads.
    /// </summary>
    public bool autoplay
    {
        get { return _autoplay; }
        set { _autoplay = (bool)value; }
    }
    private bool _loop = false;
    /// <summary>
    /// In the case of a single video player, a setting of 
    /// true will cause the player to play the initial video 
    /// again and again. In the case of a playlist player 
    /// (or custom player), the player will play the entire 
    /// playlist and then start again at the first video.
    /// </summary>
    public bool loop
    {
        get { return _loop; }
        set { _loop = (bool)value; }
    }
    private bool _enablejsapi = false;
    /// <summary>
    /// Values: 0 or 1. Default is 0. Setting this to 1 will 
    /// enable the Javascript API. For more information on 
    /// the Javascript API and how to use it, see the 
    /// JavaScript API documentation.
    /// </summary>
    public bool enablejsapi
    {
        get { return _enablejsapi; }
        set { _enablejsapi = (bool)value; }
    }
    private string _playerapiid = string.Empty;
    /// <summary>
    /// Value can be any alphanumeric string. This setting is 
    /// used in conjunction with the JavaScript API. See the 
    /// JavaScript API documentation for details.
    /// </summary>
    public string playerapiid
    {
        get { return _playerapiid; }
        set { _playerapiid = (string)value; }
    }
    private bool _disablekb = false;
    /// <summary>
    /// Values: 0 or 1. Default is 0. Setting this to 1 will 
    /// enable the Javascript API. For more information on 
    /// the Javascript API and how to use it, see the JavaScript 
    /// API documentation.        
    ///	Spacebar: Play / Pause
    /// Arrow Left: Restart current video
    /// Arrow Right: Jump ahead 10% in the current video
    /// Arrow Up: Volume up
    /// Arrow Down: Volume Down 
    /// </summary>
    public bool disablekb
    {
        get { return _disablekb; }
        set { _disablekb = (bool)value; }
    }
    private bool _egm = false;
    /// <summary>
    /// Values: 0 or 1. Default is 0. Setting to 1 enables 
    /// the "Enhanced Genie Menu". This behavior causes the 
    /// genie menu (if present) to appear when the user's 
    /// mouse enters the video display area, as opposed to 
    /// only appearing when the menu button is pressed.
    /// </summary>
    public bool egm
    {
        get { return _egm; }
        set { _egm = (bool)value; }
    }
    private string allowFullScreenParam = "<param name=\"allowFullScreen\" value=\"true\"></param>";
    private string allowFullScreenEmbedProperty = "&allowfullscreen=\"true\"";
    private bool _fs = false;
    /// <summary>
    /// Values: 0 or 1. Default is 0. Setting to 1 enables 
    /// the fullscreen button. This has no effect on the 
    /// Chromeless Player. Note that you must include some 
    /// extra arguments to your embed code for this to work. 
    /// The bolded parts of the below example enable fullscreen 
    /// functionality        
    /// </summary>
    public bool fs
    {
        get { return _fs; }
        set { _fs = (bool)value; }
    }
    private bool _showsearch = true;
    /// <summary>
    /// Values: 0 or 1. Default is 1. Setting to 0 disables the 
    /// search box from displaying when the video is minimized. 
    /// Note that if the rel parameter is set to 0 then the search 
    /// box will also be disabled, regardless of the value of 
    /// showsearch.
    /// </summary>
    public bool showsearch
    {
        get { return _showsearch; }
        set { _showsearch = (bool)value; }
    }
    private bool _showinfo = true;
    /// <summary>
    /// Values: 0 or 1. Default is 1. Setting to 0 causes the 
    /// player to not display information like the video title 
    /// and rating before the video starts playing.
    /// </summary>
    public bool showinfo
    {
        get { return _showinfo; }
        set { _showinfo = (bool)value; }
    }
    private int _iv_load_policy = 1;
    /// <summary>
    /// Values: 1 or 3. Default is 1. Setting to 1 will cause 
    /// video annotations to be shown by default, whereas 
    /// setting to 3 will cause video annotation to not be shown 
    /// by default.
    /// </summary>
    public int iv_load_policy
    {
        get { return _iv_load_policy; }
        set { _iv_load_policy = (int)value; }
    }
    private int _cc_load_policy = 0;
    /// <summary>
    /// Values: 1. Default is based on user preference. 
    /// Setting to 1 will cause closed captions to be shown 
    /// by default, even if the user has turned captions off.
    /// </summary>
    public int cc_load_policy
    {
        get { return _cc_load_policy; }
        set { _cc_load_policy = (int)value; }
    }
    protected int _playerHeight = 355;
    /// <summary>
    /// Default is 355
    /// </summary>
    public int PlayerHeight
    {
        get { return _playerHeight; }
        set { _playerHeight = (int)value; }
    }
    protected int _playerWidth = 425;
    /// <summary>
    /// Default is 425
    /// </summary>
    public int PlayerWidth
    {
        get { return _playerWidth; }
        set { _playerWidth = (int)value; }
    }
    protected string _youtubeVideoId = string.Empty;
    /// <summary>
    /// Video ID
    /// </summary>
    public string YoutubeVideoId
    {
        get { return _youtubeVideoId; }
        set { _youtubeVideoId = (string)value; }
    }
    #endregion
}
